package com.aiyige.page.my.message.notifymessage.likemessage;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.Router;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.api.ApiManager;
import com.aiyige.model.Page;
import com.aiyige.model.message.entity.Message;
import com.aiyige.model.message.view.PraiseContent;
import com.aiyige.model.moment.entity.Moment;
import com.aiyige.page.my.message.notifymessage.likemessage.adapter.LikeMessageAdapter;
import com.aiyige.page.my.message.notifymessage.likemessage.model.LikeMessage;
import com.aiyige.page.my.message.util.MessageUtil;
import com.aiyige.utils.widget.CommonDataView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;

@Route(path = ARouterConfig.LikeMessagePage)
public class LikeMessagePage extends AppCompatActivity implements CommonDataView.RetrofitStub {

    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.cdv)
    CommonDataView cdv;
    LikeMessageAdapter likeMessageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_like_message);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        titleTv.setText(R.string.like);
        likeMessageAdapter = new LikeMessageAdapter();
        likeMessageAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                LikeMessage likeMessage = likeMessageAdapter.getItem(position);
                Router.start(likeMessage.getRouter());
            }
        });
        likeMessageAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.otherHeadIv:
                    case R.id.otherNameTv:
                        LikeMessage likeMessage = likeMessageAdapter.getItem(position);
                        ARouter.getInstance()
                                .build(ARouterConfig.UserPage)
                                .withString("userId",likeMessage.getOtherId())
                                .navigation();
                        break;
                }
            }
        });
        cdv.config(likeMessageAdapter, this);
        cdv.doRefreshRequest();
    }

    @OnClick({R.id.titleBackBtn})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.titleBackBtn:
                finish();
                break;
        }
    }

    @Override
    public Call<ResponseBody> call(long currentPage) {
        return ApiManager.getService().message(Message.TYPE_PRAISE, 15, currentPage);
    }

    @Override
    public CommonDataView.HandleResult handleResponse(String respJson, List resultList) {
        try {
            Page page = JSON.parseObject(respJson, Page.class);
            List<Message> messageList = new LinkedList<>();
            if (!TextUtils.isEmpty(page.getContent())) {
                messageList.addAll(JSON.parseArray(page.getContent(), Message.class));
            }
            for (Message message : messageList) {
                PraiseContent record = null;
                try {
                    record = message.getContent().toJavaObject(PraiseContent.class);
                } catch (Exception e) {

                }
                if (record == null) {
                    continue;
                }
                int targetType;
                if (record.getComment() != null) {
                    targetType = MessageUtil.TARGET_TYPE_COMMENT;
                } else {
                    switch (record.getMoment().getSubject()) {
                        case Moment.SUBJECT_ARTICLE:
                            targetType = MessageUtil.TARGET_TYPE_INFO;
                            break;
                        case Moment.SUBJECT_VIDEO:
                            targetType = MessageUtil.TARGET_TYPE_VIDEO;
                            break;
                        case Moment.SUBJECT_PHOTO:
                            targetType = MessageUtil.TARGET_TYPE_PHOTO;
                            break;
                        case Moment.SUBJECT_VIDEO_COURSE:
                            targetType = MessageUtil.TARGET_TYPE_LEARN_VIDEO;
                            break;
                        case Moment.SUBJECT_MAJOR_COURSE:
                            targetType = MessageUtil.TARGET_TYPE_MAJOR_COURSE;
                            break;
                        case Moment.SUBJECT_SPECIAL_TOPIC:
                            targetType = MessageUtil.TARGET_TYPE_SPECIAL_TOPIC;
                            break;
                        case Moment.SUBJECT_TRAINING_COURSE:
                            targetType = MessageUtil.TARGET_TYPE_TRAING_CARD;
                            break;
                        case Moment.SUBJECT_PRIVATE_COURSE:
                            targetType = MessageUtil.TARGET_TYPE_PERSONAL_TRAINER;
                            break;
                        default:
                            targetType = MessageUtil.TARGET_TYPE_COMMENT;
                            break;
                    }
                }

                String myComment = "";
                try {
                    myComment = record.getComment().getContent();
                } catch (Exception e) {

                }
                String myName = "";
                try {
                    myName = record.getComment().getUser();
                } catch (Exception e) {

                }
                resultList.add(LikeMessage.newBuilder()
                        .otherAvatar(record.getSender().getAvatar())
                        .otherId(record.getSender().getId())
                        .otherName(record.getSender().getName())
                        .goodsId(record.getMoment().getId())
                        .otherTime(message.getCreateTime())
                        .mediaReleaseDate(record.getMoment().getCreateTime())
                        .cover(record.getMoment().getCover())
                        .targetType(targetType)
                        .mediaTitle(record.getMoment().getTitle())
                        .myComment(myComment)
                        .myName(myName)
                        .router(record.getMoment().getRouter())
                        .router(record.getMoment().getRouter())
                        .build());
            }
            return new CommonDataView.HandleResult(page.isLast());
        } catch (Exception e) {
            return new CommonDataView.HandleResult(e.getMessage());
        }
    }
}
